package org.jeecg.modules.jxc.basicdata.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.models.auth.In;
import org.jeecg.modules.jxc.basicdata.entity.Goods;
import org.jeecg.modules.jxc.basicdata.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yaorange
 * @since 2021-10-27
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private IGoodsService goodsService;

    /**
     * 商品管理搜索分页查询
     * @param key
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getGoodsAndType")
    public ResponseEntity<?> getGoodsAndType(@RequestParam(value = "key",required = false) String key,
                                             @RequestParam(value = "pageNo") Integer pageNo,
                                             @RequestParam(value = "pageSize") Integer pageSize){
        Page<Goods> page = new Page<>(pageNo, pageSize);
        IPage<Goods> goodsAndType = goodsService.getGoodsAndType(page, key);
        return ResponseEntity.ok(goodsAndType);
    }

    /**
     * 根据id删除对应goods
     * @param id
     * @return
     */
    @DeleteMapping("/deleteGoods/{id}")
    public ResponseEntity<?> deleteGoods(@PathVariable(value = "id") Integer id){
        boolean b = goodsService.removeById(id);
        return ResponseEntity.ok(b);
    }

    /**
     * 根据传来的goods对象，新增或者修改数据库
     * @param goods
     * @return
     */
    @PostMapping("/updateGoods")
    public ResponseEntity<?> updateGoods(@RequestBody Goods goods){
        boolean b = goodsService.saveOrUpdate(goods);
        return ResponseEntity.ok(b);
    }

    @GetMapping("/treeGetGoods")
    public ResponseEntity<?> treeGetGoods(@RequestParam(value = "goodsTypeId") Integer goodsTypeId,
                                          @RequestParam(value = "pageNo") Integer pageNo,
                                          @RequestParam(value = "pageSize") Integer pageSize){
        Page<Goods> page = new Page<>(pageNo,pageSize);
        IPage<Goods> iPage = goodsService.treeGetGoods(page,goodsTypeId);
        return ResponseEntity.ok(iPage);
    }

    @GetMapping("/getGoodsById/{id}")
    public ResponseEntity<?> getGoodsById(@PathVariable(value = "id") Integer id){
        Goods goods = goodsService.getById(id);
        return ResponseEntity.ok(goods);
    }
}

